<template>
  <div id="questionSubmitView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="questionId" label="题号" style="min-width: 240px">
        <a-input v-model="searchParams.questionId" placeholder="请输入" />
      </a-form-item>
      <a-form-item field="language" label="编程语言" style="min-width: 240px">
        <a-select
          v-model="searchParams.language"
          :style="{ width: '320px' }"
          placeholder="选择编程语言"
        >
          <a-option>java</a-option>
          <a-option>c++</a-option>
          <a-option>python</a-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSubmit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      :ref="tableRef"
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #id="{ record }">
        <div @click="handleClick(record.id)">{{ record.id }}</div>
        <a-modal
          v-model:visible="visibleMap[`${record.id}`]"
          :mask-style="{ backgroundColor: 'rgba(0, 0, 0, 0.1)' }"
          :closable="false"
          :footer="null"
          class="custom-modal"
        >
          <pre v-if="codeMap[`${record.id}`]"
            >{{ codeMap[`${record.id}`] }}
          </pre>
          <div v-else>Loading...</div>
        </a-modal>
      </template>
      <template #message="{ record }">
        <span :style="getMessageColor(record.judgeInfo.message)">{{
          record.judgeInfo.message
        }}</span>
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY/MM/DD") }}<br />
        {{ moment(record.createTime).format("HH:mm:ss") }}
      </template>
      <template #questionVO="{ record }">
        <a-link @click="toQuestionPage(record.questionVO)"
          >{{ record.questionVO.title }}
        </a-link>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionSubmitQueryRequest,
  QuestionVO,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";

const tableRef = ref();

const visibleMap = ref({});
const codeMap = ref({});
const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionSubmitQueryRequest>({
  questionId: undefined,
  language: undefined,
  pageSize: 10,
  current: 1,
});

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    {
      ...searchParams.value,
      sortField: "createTime",
      sortOrder: "descend",
    }
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
  }
};

const getData = async (submitId: number) => {
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingGet(
    submitId
  );
  console.log(res.data);
  if (res.code === 0) {
    return res.data;
  } else {
    message.error("加载失败，" + res.message);
  }
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

const handleClick = async (id: number) => {
  const key = `${id}`;
  visibleMap.value[key] = !visibleMap.value[key];
  if (visibleMap.value[key] && !codeMap.value[key]) {
    const data = await getData(id);
    codeMap.value[key] = data;
  }
};

const getMessageColor = computed(() => (message: string) => {
  if (message.includes("Accepted")) {
    return "color: green; font-size: 16px; margin: 0 10px;";
  } else if (message.includes("Waiting")) {
    return "color: grey; font-size: 16px; margin: 0 10px;";
  } else {
    return "color: red; font-size: 16px; margin: 0 10px;";
  }
});

const columns = [
  {
    title: "#",
    slotName: "id",
  },
  {
    title: "时间",
    slotName: "createTime",
  },
  {
    title: "用户",
    dataIndex: "userVO.userName",
  },
  {
    title: "题目",
    slotName: "questionVO",
  },
  {
    title: "编程语言",
    dataIndex: "language",
  },
  {
    title: "状态",
    slotName: "message",
  },
  {
    title: "时间(ms)",
    dataIndex: "judgeInfo.time",
  },
  {
    title: "内存(KB)",
    dataIndex: "judgeInfo.memory",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const router = useRouter();

/**
 * 跳转到做题页面
 * @param question
 */
const toQuestionPage = (questionVO: QuestionVO) => {
  console.log("哈喽，我跳转了");
  router.push({
    path: `/view/question/${questionVO.id}`,
  });
};

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  // 这里需要重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
};
</script>

<style scoped>
#questionSubmitView {
  max-width: 1280px;
  margin: 0 auto;
}

.custom-modal .arco-modal-content {
  width: 1400px;
}

.custom-modal .arco-modal-body {
  min-height: 300px;
}
</style>
